package com.hjm.project.accounts.accounts.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.constant.DictConstants;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.framework.cache.RedisCache;
import com.hjm.framework.cache.RedisUtils;
import com.hjm.project.area.domain.Area;
import com.hjm.project.area.service.IAreaService;
import com.yxhd.encrypt.EncryptUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.accounts.accounts.domain.Accounts;
import com.hjm.project.accounts.accounts.service.IAccountsService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 商户结算账户Controller
 * 
 * @author LOVE
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/accounts/accounts")
public class AccountsController extends BaseController
{
    private String prefix = "accounts/accounts";

    @Autowired
    private IAccountsService accountsService;

    @Autowired
    private IAreaService areaService;       //区域
    @Autowired
    private RedisCache redisCache;

    @RequiresPermissions("accounts:accounts:view")
    @GetMapping()
    public String accounts()
    {
        return prefix + "/accounts";
    }

    /**
     * 查询商户结算账户列表
     */
    @RequiresPermissions("accounts:accounts:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Accounts accounts) throws Exception
    {
        startPage();
        if (StringUtils.isNotEmpty(accounts.getResphone())){
            accounts.setResphone(EncryptUtils.getEncrypt(accounts.getResphone()));
        }
        List<Accounts> list = accountsService.selectAccountsList(accounts);
        return getDataTable(list);
    }

    /**
     * 导出商户结算账户列表
     */
    @RequiresPermissions("accounts:accounts:export")
    @Log(title = "商户结算账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Accounts accounts)
    {
        List<Accounts> list = accountsService.selectAccountsList(accounts);
        ExcelUtil<Accounts> util = new ExcelUtil<Accounts>(Accounts.class);
        return util.exportExcel(list, "accounts");
    }

    /**
     * 新增商户结算账户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商户结算账户
     */
    @RequiresPermissions("accounts:accounts:add")
    @Log(title = "商户结算账户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Accounts accounts)
    {
        return toAjax(accountsService.insertAccounts(accounts));
    }

    /**
     * 修改商户结算账户
     */
    @GetMapping("/edit/{daMarker}")
    public String edit(@PathVariable("daMarker") Long daMarker, ModelMap mmap)
    {
        Accounts accounts = accountsService.selectAccountsById(daMarker);
        mmap.put("accounts", accounts);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户结算账户
     */
    @RequiresPermissions("accounts:accounts:edit")
    @Log(title = "商户结算账户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Accounts accounts)
    {
        return toAjax(accountsService.updateAccounts(accounts));
    }

    /**
     * 删除商户结算账户
     */
    @RequiresPermissions("accounts:accounts:remove")
    @Log(title = "商户结算账户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(accountsService.deleteAccountsByIds(ids));
    }


    @RequiresPermissions("accounts:accounts:merAccounts")
    @GetMapping("/merAccounts")
    public String merAccounts()
    {
        return prefix + "/merAccounts";
    }

    /**
     * 商户修改注册地址
     */
    @GetMapping("/updateSettle/{id}")
    public String updateSettle(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        Accounts accounts = accountsService.selectAccountsById(id);
        accounts.setScreenNum(EncryptUtils.getDecrypt(accounts.getScreenNum()));
        accounts.setResphone(EncryptUtils.getDecrypt(accounts.getResphone()));
        accounts.setScreenIdnum(EncryptUtils.getDecrypt(accounts.getScreenIdnum()));

        String addressName = accounts.getAreaName();
        String province="";
        String city="";
        String areaName="";
        if (StringUtils.isNotEmpty(addressName)){
            String[] addressArray = addressName.split(",");
            if(addressArray.length>1){
                if (StringUtils.isNotEmpty(addressArray[0])){
                    province=addressArray[0];
                    Map<String,Object> params=new HashMap();
                    params.put("area_name",province);
                    params.put("area_type","0");
                    Map<String,Object> areaCodeMap=areaService.findQBAreaCode(params);
                    province=String.valueOf(areaCodeMap.get("AREA_CODE"));
                    mmap.put("province", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
                if (StringUtils.isNotEmpty(addressArray[1])){
                    city=addressArray[1];
                    Map<String,Object> params=new HashMap();
                    params.put("area_name",city);
                    params.put("area_type","1");
                    Map<String,Object> areaCodeMap=areaService.findQBAreaCode(params);
                    city=String.valueOf(areaCodeMap.get("AREA_CODE"));
                    mmap.put("city", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
            }
            if (addressArray.length>2){
                if (StringUtils.isNotEmpty(addressArray[2])){
                    areaName=addressArray[2];
                    Map<String,Object> params=new HashMap();
                    params.put("area_name",areaName);
                    params.put("area_type","2");
                    Map<String,Object> areaCodeMap=areaService.findQBAreaCode(params);
                    mmap.put("area", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
            }
        }
        List<Area> pList = new ArrayList<>();
        List<Area> cList = new ArrayList<>();
        List<Area> aList = new ArrayList<>();
        List<Area> areas =redisCache.getCacheList(DictConstants.getAreaKey());
        if (areas==null){
            areas = areaService.selectAreaList(null);
            redisCache.setCacheList(DictConstants.getAreaKey(), areas);
        }
        //List<Area> areas = areaService.selectAreaList(null);
        for (Area area : areas) {
            if (area.getAreaType() == 0) {
                pList.add(area);
            } else if (area.getAreaType() == 1 && province.equals(area.getParentId())){
                cList.add(area);
            } else if (area.getAreaType() == 2 && city.equals(area.getParentId())){
                aList.add(area);
            }
        }
        mmap.put("pList",pList);
        mmap.put("cList",cList);
        mmap.put("aList",aList);
        mmap.put("accounts", accounts);
        return prefix + "/updateSettle";
    }
    /**
     * 保存账户信息
     */
    @PostMapping("/updateSettle")
    @RequiresPermissions("agentInfo:agentInfo:updateSettle")
    @Log(title = "账户信息", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult updateSettle(@RequestParam Map<String,String> map) throws Exception
    {
        map.put("createman",this.getSysUser().getLoginName());
        map.put("createdate", DateUtils.getDate());
        map.put("identity","insert");
        return accountsService.updateSettle(map);
    }
    /**
     * 支行查找带回
     */
    @GetMapping("/selectBranchBank")
    public String selectBranchBank()
    {
        return prefix + "/selectBranchBank";
    }
}
